package com.cytech.cloudclinic.moudle.main.clinic.admission.prescription.template;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Parcelable;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.entity.node.BaseNode;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.cytech.cloudclinic.R;
import com.cytech.cloudclinic.adapter.PrescriptionTemplateDrugAdapter;
import com.cytech.cloudclinic.adapter.TemplateAdapter;
import com.cytech.cloudclinic.adapter.provide.FirstNode;
import com.cytech.cloudclinic.adapter.provide.SecondNode;
import com.cytech.cloudclinic.base.BaseActivity;
import com.cytech.cloudclinic.bean.Drug;
import com.cytech.cloudclinic.bean.PrescriptionTag;
import com.cytech.cloudclinic.bean.PrescriptionTemplate;
import com.cytech.cloudclinic.bean.event.PrescriptionTagClick;
import com.cytech.cloudclinic.bean.event.RefreshAction;
import com.yechaoa.yutils.ToastUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 处方模板
 */
public class PrescriptionTemplateActivity extends BaseActivity<PrescriptionTemplatePresenter> implements PrescriptionTemplateView {

    @BindView(R.id.rv_prescription_template_menu)
    RecyclerView menuRv;
    @BindView(R.id.rv_prescription_template_drug)
    RecyclerView drugRv;
    @BindView(R.id.titlebar_title)
    TextView tvTitle;
    @BindView(R.id.tv_prescription_tag_name)
    TextView tagNameTv;

    private List<Drug> drugList;
    private List<BaseNode> menuList;
    private TemplateAdapter templateAdapter;
    private PrescriptionTemplateDrugAdapter drugAdapter;

    private int drugParentTag;

    public static void actionStart(Context context,int drugParentTag){
        Intent intent = new Intent(context,PrescriptionTemplateActivity.class);
        intent.putExtra("drugParentTag",drugParentTag);
        context.startActivity(intent);
    }


    @Override
    protected PrescriptionTemplatePresenter createPresenter() {
        return new PrescriptionTemplatePresenter(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.act_prescription_template;
    }

    @Override
    protected void initView() {
        EventBus.getDefault().register(this);
        tvTitle.setText("处方模板");
        drugParentTag=getIntent().getIntExtra("drugParentTag",0);
    //////
        initDrugRv();


    }

    @Override
    protected void initData() {
        presenter.getPrescriptionTags(drugParentTag);
    }

    @Override
    public void getPrescriptionSuccess(List<PrescriptionTag> result) {
       List<BaseNode> menuList = new ArrayList<>();
        for (int i = 0; i < result.size(); i++) {
            //SecondNode 的 list
            List<BaseNode> secondNodeList = new ArrayList<>();
            for (int n = 0; n < result.get(i).diagnoses.size(); n++) {
                SecondNode seNode = new SecondNode(result.get(i).diagnoses.get(n).diagnose,result.get(i).diagnoses.get(n).prescription_id,result.get(i).diagnoses.get(n).prescription_type);
                secondNodeList.add(seNode);
            }

            FirstNode entity = new FirstNode(secondNodeList, result.get(i).value);
            entity.setExpanded(i == 0);
            menuList.add(entity);
        }
        templateAdapter = new TemplateAdapter();
        templateAdapter.setNewInstance(menuList);
        menuRv.setLayoutManager(new LinearLayoutManager(this));
        menuRv.setAdapter(templateAdapter);
        templateAdapter.setOnItemChildClickListener(new OnItemChildClickListener() {
            @Override
            public void onItemChildClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
                ToastUtil.showToast(""+position);
            }
        });

        if(result!=null&&result.size()>0){
            List<PrescriptionTemplate> list = result.get(0).diagnoses;
            if(list!=null&&list.size()>0){
                tagNameTv.setText(list.get(0).diagnose);
                presenter.getPrescriptionItem(list.get(0).prescription_id);
            }
        }
    }

    @Override
    public void getPrescriptionDrugSuccess(List<Drug> drugList) {
        drugAdapter.setNewInstance(drugList);
    }

    private void initDrugRv(){
        drugList = new ArrayList<>();
        drugAdapter = new PrescriptionTemplateDrugAdapter(drugList);
        drugRv.setLayoutManager(new LinearLayoutManager(this));
        drugRv.setAdapter(drugAdapter);
    }

    @OnClick(R.id.titlebar_back)
    public void exit(View view){
        finish();
    }

    @OnClick(R.id.btn_template_import)
    public void importTemplate(View view){
        Intent intent = new Intent();
        List<Drug> drugs = drugAdapter.getData();
        intent.putExtra("result", (Serializable) drugs);
        setResult(Activity.RESULT_OK,intent);
        finish();
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void updateData(PrescriptionTagClick event) {
        presenter.getPrescriptionItem(event.id);
        tagNameTv.setText(event.text);
    }

}
